package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import beans.Comment;
import beans.Favourite;
import beans.Follow;
import beans.Restaurant;
import beans.User;

import dao.CommentDAO;
import dao.FavouriteDAO;
import dao.FollowDAO;
import dao.RateDAO;
import dao.RestaurantDAO;
import dao.RestaurantTagDAO;


@Controller

public class RestaurantController{

	private RestaurantDAO restaurantDAO;
	private RestaurantTagDAO restaurantTagDAO;
	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	public void setRestaurantTagDAO(RestaurantTagDAO restaurantTagDAO) {
		this.restaurantTagDAO = restaurantTagDAO;
	}
	private FavouriteDAO favouriteDAO;
	private LoggedUser loggedUser;	
	private FollowDAO followDAO;
	private CommentDAO commentDAO;
	private RateDAO rateDAO;
	

	public void setRateDAO(RateDAO rateDAO) {
		this.rateDAO = rateDAO;
	}

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

	public void setLoggedUser(LoggedUser loggedUser) {
		this.loggedUser = loggedUser;
	}
	


	public void setFavouriteDAO(FavouriteDAO favouriteDAO) {
		this.favouriteDAO = favouriteDAO;
	}


	@RequestMapping(value = "/restaurantid={restaurantId}", method=RequestMethod.GET)
	public ModelAndView restaurant(@PathVariable("restaurantId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Restaurant restaurant = restaurantDAO.findRestaurantById(id);
		ModelMap modelMap = new ModelMap();
		String logosource;
		 if (restaurant.getName() != null) {
		   logosource = "data:image/jpeg;base64,"
		     + (restaurant.getLogo()).toString();
		  } 
		 else {
		   logosource = "";
		  }
		String tagi = restaurantTagDAO.GetAllTagsFromRestaurant(restaurant);
		modelMap.addAttribute("tagi", tagi);				   
		modelMap.addAttribute("restaurant", restaurant);
		modelMap.addAttribute("logosource", logosource);
		User user = loggedUser.getLoggedUser();
		Favourite favourite = favouriteDAO.findFavourite(user, restaurant);
		boolean favouriteChecked;
		if(favourite==null){
			favouriteChecked=false;
		}
		else{
			favouriteChecked=true;
		}
		modelMap.addAttribute("favouriteChecked",favouriteChecked);
		boolean followChecked;
		Follow follow = followDAO.findFollow(user,restaurant);
		if(follow==null){
			followChecked=false;
		}
		else{
			followChecked=true;
		}
		modelMap.addAttribute("followChecked",followChecked);
		modelMap.addAttribute("comment", new Comment());
		modelMap.addAttribute("user", user);
		modelMap.addAttribute("commentList", commentDAO.GetAllCommentsFromRestaurant(restaurant));
		modelMap.addAttribute("rate", rateDAO.findRate(user, restaurant.getId()));
		return new ModelAndView("restaurant", modelMap);
	}
	
	
}

